/**
 * 站内消息或公告
 */
var notice = function() {
    var self = this;
    self.list = [];

    // 当前显示的消息
    self.currIndex = 0;
};
notice.prototype = {};

notice.prototype.init = function(cb) {
    var self = this;
    self.cb = cb;

    var f = function() {
        // 拉取所有未读消息
        CMD.notice_uquery(undefined, true, 20, 1, function(r) {
            self.list = r.list;
            if (self.list.length > 0) self.show();
            if (cb) cb();
        });
    }
    f();
}

notice.prototype.show = function() {
    var self = this;
    self.draw();

    if (self.phone) {
        Message.popup();
    }
    else {
        $("#message_content").css("max-height", ($(window).height() - 120) + "px");
        $("#message").modal({
            width: 600,
        });
    }
    
    $("#message button").click(function() {
        // 标记已读
        if ("首页弹窗" != self.list[self.currIndex].type ||
            !self.list[self.currIndex].forever)
            CMD.notice_flag_read(self.list[self.currIndex].id);

        if (self.currIndex >= self.list.length - 1) {
            $("#message").modal("close");
        }
        else {
            self.currIndex++;
            self.draw();
        }
    });
}
notice.prototype.draw = function() {
    var self = this;
    var data = self.list[self.currIndex];
    data = Util.convertNotice(data);
    $("#message_content .title").html(data.subject);
    $("#message_content .subtitle").html(data.time);
    $("#message_content .content").html(data.content);

    if (self.currIndex >= self.list.length - 1) {
        $("#message button").html("关闭");
    }
    else {
        $("#message button").html("下一条");
    }
}

// 公告内容（非弹窗消息）
notice.prototype.queryNotices = function(ul) {
    Classify.queryExt("settings", "board", true, [], function(v) {
        var list = v;
        list.sort(function(a, b) {
            return b.sort - a.sort;
        })

        ul.empty();
        for (var i in list) {
            var li = '<li>' + list[i].content + '</li>';
            ul.append(li);
        }
        ul.marquee("update");
        ul.marquee();
    });
}

window.Notice = new notice();
